package json

import (
	"encoding/json"
	"fmt"
)

type Person struct {
	Name  string
	Hobby string
}

func to_json() {
	p := Person{"5lmh.com", "女"}
	fmt.Println("person:", p)
	fmt.Printf("person: %T", p)

	// 编码json
	b, err := json.Marshal(p)
	if err != nil {
		fmt.Println("json err ", err)
	}

	// 格式化输出
	b, err = json.MarshalIndent(p, "", "     ")
	if err != nil {
		fmt.Println("json err ", err)
	}
	fmt.Println("json:\n", string(b))
	fmt.Printf("json: %T", b)
}

type Student struct {
	//"-"是忽略的意思
	Name string `json:"name"`
	Age  int    `json:"age" `
	sex  string `json:"sex" `
}

func map_to_json() {
	student := make(map[string]interface{})
	student["name"] = "5lmh.com"
	student["age"] = "18"
	student["sex"] = "man"
	fmt.Println("student:", student)
	b, err := json.Marshal(student)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("json:", b)

	var p map[string]string

	errJson := json.Unmarshal(b, &p)
	if errJson != nil {
		fmt.Println(errJson)
	}
	fmt.Println("json-map:", p)
}
